package commMqttRpc

import (
	"errors"
	"sync"
)

var (
	mutex   sync.RWMutex
	client  *mqttClient
	handler *mqttHandler
)

func init() {
	client = &mqttClient{}
	handler = &mqttHandler{}
}

func Instance(mqtt map[string]interface{}, edgeId, request, respond string) {
	client.Instance(mqtt, edgeId, request, respond)
}

func Request(requestVO *RestfulLikeRequestVO) (respondVO *RestfulLikeRespondVO, err error) {
	// 限制并发
	mutex.Lock()
	defer mutex.Unlock()

	if client.client == nil || !client.client.IsConnected() {
		return nil, errors.New("mqtt尚未建立连接")
	}

	return client.request(requestVO)
}
